package cn.com.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.entity.Pexamination;
import cn.com.entity.PexaminationPackage;



public class PexaminationDao {
	
	private SessionFactory sessionFac;

	public SessionFactory getSessionFac() {
		return sessionFac;
	}

	public void setSessionFac(SessionFactory sessionFac) {
		this.sessionFac = sessionFac;
	}
	
	Session session;//必须创建session
	Transaction tr;//如果只有查询的就用不到这个
	//体检查询
/*	public List<Pexamination> selectPexamination(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		System.out.println("你丫的是不是欠揍呀？");
//		session=sessionFac.openSession();
		String hql="from Pexamination";
		Query query=session.createQuery(hql);
		System.out.println("kaishichaxun");
		List<Pexamination> list=query.list();
		return list;
	}*/
	//增加体检
	public boolean addPexamination(Pexamination pexamination){
		boolean b=false;
		
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.save(pexamination);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//删除体检
	public boolean deletePexamination(Pexamination pexamination){
		boolean b=false;
			try {
				session=sessionFac.openSession();
				tr=session.beginTransaction();
				session.delete(pexamination);
				b=true;
				tr.commit();
				session.close();
				return b;
			} catch (HibernateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return b;
			}
	}
	//修改体检
	public boolean updatePexamination(Pexamination pexamination){
		boolean b=false;
		try {
			session=sessionFac.openSession();
			tr=session.beginTransaction();
			session.update(pexamination);
			tr.commit();
			session.close();
			b=true;
			return b;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return b;
		}
	}
	//查询全部体检
	public List<Pexamination> selectPexamination(){
		session=sessionFac.openSession();
		String hql="from Pexamination";
		Query query=session.createQuery(hql);
		List<Pexamination> list=query.list();
		if(list!=null){
			System.out.println("en en enenenene");
		}
		return list;
	}
	//查询一个人的
	public Pexamination selectPexaminationByid(Pexamination pexamination){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		String hql="from Pexamination where pexaminationid=?";
		Query query=session.createQuery(hql);
		query.setInteger(0,pexamination.getPexaminationid());
		List<Pexamination> list=query.list();
		if(list!=null&&list.size()==1){
			pexamination=list.get(0);
			System.out.println("ole");
		}
		return pexamination;
	}
	//查询类别
	public Pexamination getonebyid(int pxid){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		sessionFac=(SessionFactory)ctx.getBean("sessionFac");
		session=sessionFac.openSession();
		Pexamination px=(Pexamination)session.get(Pexamination.class, pxid);
		session.close();
		return px;
	}
	
}
